package com.example.newsapp.ui.components.otherComponent.searchComponent

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.newsapp.R
import com.example.newsapp.data.compositionLocal.LocalFocusRequest
import com.example.newsapp.data.compositionLocal.LocalNavController
import com.example.newsapp.data.database.NewsItems
import com.example.newsapp.data.`object`.PageNavigation
import com.example.newsapp.data.`object`.StateSave

@Composable
fun HotSearchList(
    hotNumsList: List<NewsItems>,
    modifier: Modifier = Modifier
) {
    var focusRequester = LocalFocusRequest.current
    var navController = LocalNavController.current
    val keyboardController = LocalSoftwareKeyboardController.current
    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(16.dp)
            .pointerInput(Unit) {
                keyboardController?.hide()
            }
            .clickable(
                interactionSource = null,
                indication = null
            ) {
                StateSave.hotInitState++
                focusRequester.freeFocus()
                keyboardController?.hide()
            },
        verticalArrangement = Arrangement.spacedBy(8.dp)
    ) {
        Row(
            modifier = Modifier.fillMaxWidth(),
            verticalAlignment = Alignment.CenterVertically
        ) {
            Icon(
                painter = painterResource(R.drawable.baseline_local_fire_department_24),
                contentDescription = "热门搜索图标",
                tint = Color.Red,
                modifier = Modifier.size(24.dp)
            )
            Text(
                text = "热门搜索",
                fontSize = 20.sp,
                fontWeight = FontWeight.Bold,
                modifier = Modifier.padding(start = 8.dp)
            )
        }
        hotNumsList.forEachIndexed { index, newsItem ->
            Row(
                modifier = Modifier.fillMaxWidth()
                    .clickable {
                        navController.navigate("${PageNavigation.searchResult}?title=${newsItem.title}")
                    },
                verticalAlignment = Alignment.CenterVertically
            ) {
                Text(
                    text = "${index + 1}",
                    style = MaterialTheme.typography.bodyLarge,
                    fontWeight = if (index < 3) FontWeight.Bold else null,
                    color = if (index < 3) Color.Red else MaterialTheme.colorScheme.onSurface,
                    modifier = Modifier.width(24.dp),
                    textAlign = TextAlign.Center
                )
                Box(modifier = Modifier.fillMaxWidth()) {
                    Text(
                        text = newsItem.title,
                        style = MaterialTheme.typography.bodyLarge,
                        modifier = Modifier.padding(start = 8.dp, end = if (index < 3) 35.dp else 0.dp),
                        maxLines = 1,
                        overflow = TextOverflow.Ellipsis
                    )
                    if (index < 3) {
                        Icon(
                            painter = painterResource(R.drawable.hot_icon),
                            contentDescription = null,
                            tint = Color.Red,
                            modifier = Modifier.size(29.dp, 20.dp).align(Alignment.CenterEnd)
                        )
                    }
                }
            }
        }
    }
}